/**
 * 
 */
package com.gr.staffpm.pages.management.review;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.menuw.horizontal.dropdown.multilevel.MultiLevelMenu;
import com.gr.menuw.menu.item.AjaxLinkMenuItem;
import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.TaskQueue;
import com.gr.staffpm.pages.management.review.command.AcceptCommand;
import com.gr.staffpm.pages.management.review.command.IReviewCommandReciever;
import com.gr.staffpm.pages.management.review.command.RejectCommand;
import com.gr.staffpm.pages.management.review.command.SubmitFeedbackCommand;
import com.gr.staffpm.pages.tasks.AjaxReviewLink;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.service.SortableTaskQueueDataProvider;
import com.gr.staffpm.tasks.service.TaskQueueService;
import com.gr.staffpm.widget.dialog.feedback.AjaxFeedbackDialog;
import com.gr.staffpm.widget.dialog.feedback.AjaxRejectDialog;
import com.gr.staffpm.widget.jqgrid.component.Grid;
import com.gr.staffpm.widget.jqgrid.component.event.OnTaskQueueSelectRowAjaxEvent;
import com.gr.staffpm.widget.jqgrid.model.GridColumnModel;
import com.gr.staffpm.widget.jqgrid.model.GridModel;
import com.gr.staffpm.widget.jqgrid.model.GridModel.HorizontalPosition;
import com.gr.staffpm.widget.jqgrid.model.ICellPopulator;
import com.gr.staffpm.widget.jqgrid.model.PropertyPopulator;
import com.gr.staffpm.widget.jqgrid.model.TaskCellLink;

/**
 * @author Graham Rhodes 16 Jan 2011 15:56:37
 */
@MountHybrid
@MountPath(path = "secure/management/TaskReviewQueue")
public class TaskReviewPage extends StaffPMWebPage implements IReviewCommandReciever {

    @SpringBean(name = "taskQueueService")
    private TaskQueueService taskQueueService;
    @SpringBean(name = "userService")
    private UserService userService;
    private Grid<TaskQueue> grid;
    private GridModel<TaskQueue> model;
    private final List<IModel<TaskQueue>> selectedQueuedTaskModels = new ArrayList<IModel<TaskQueue>>();
    private SortableTaskQueueDataProvider dp;
    private AjaxFeedbackDialog feedbackDialog;
    private AjaxRejectDialog rejectDialog;

    public TaskReviewPage() {

        add(buildMenu());

        model = new GridModel<TaskQueue>();

        model.addColumnModel(new GridColumnModel<TaskQueue>(TaskQueue.TASK, TaskQueue.TASK, new Model<String>("Task"), 200) {

            private static final long serialVersionUID = 1L;

            @Override
            public ICellPopulator<TaskQueue> getCellPopulator() {
                return new PropertyPopulator<TaskQueue>("name") {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void populateItem(Item<ICellPopulator<TaskQueue>> cellItem, String componentId, int row, int col, IModel<TaskQueue> rowModel) {
                        cellItem.add(new TaskCellLink(componentId, new Model<Task>(rowModel.getObject().getTask())));
                    }
                };
            }
        });
        model.addColumnModel(new GridColumnModel<TaskQueue>("createdBy.fullName", TaskQueue.CREATED_BY, new Model<String>("Requester"), 200));

        model.setPagerpos(HorizontalPosition.center);
        model.setCaption("Task Review Queue");
        model.setHidegrid(false);
        model.setAutowidth(true);
        model.setHeight("400px");
        model.setMultiselect(true);

        dp = new SortableTaskQueueDataProvider(taskQueueService, userService.getCurrentUser());
        grid = new Grid<TaskQueue>("grid", model, dp);
        grid.addEvent(new OnTaskQueueSelectRowAjaxEvent(this));
        add(grid);

        feedbackDialog = new AjaxFeedbackDialog("feedbackDialog", new SubmitFeedbackCommand(this));
        addOrReplace(feedbackDialog);
        rejectDialog = new AjaxRejectDialog("rejectDialog", new SubmitFeedbackCommand(this));
        addOrReplace(rejectDialog);

    }

    @Override
    public String getTitle() {
        return "Task Review Queue - StaffPM";
    }

    private MultiLevelMenu buildMenu() {
        MultiLevelMenu operationsMenu = new MultiLevelMenu("options-menu");

        AjaxLinkMenuItem acceptMenuItem = new AjaxLinkMenuItem("1", "Accept", new AjaxReviewLink(new AcceptCommand(this)));
        operationsMenu.addItem(acceptMenuItem);

        AjaxLinkMenuItem rejectMenuItem = new AjaxLinkMenuItem("2", "Reject", new AjaxReviewLink(new RejectCommand(this)));
        operationsMenu.addItem(rejectMenuItem);

        return operationsMenu;
    }

    public List<IModel<TaskQueue>> getSelectedQueuedTaskModels() {
        return selectedQueuedTaskModels;
    }

    @Override
    public void rejectTasks(AjaxRequestTarget target) {
        if (!selectedQueuedTaskModels.isEmpty()) {
            rejectDialog.setQueuedTasks(selectedQueuedTaskModels);
            rejectDialog.initialise(target);
            rejectDialog.show(target);
        }
    }

    @Override
    public void acceptTasks(AjaxRequestTarget target) {
        if (!selectedQueuedTaskModels.isEmpty()) {
            feedbackDialog.setQueuedTasks(selectedQueuedTaskModels);
            feedbackDialog.initialise(target);
            feedbackDialog.show(target);
        }
    }

    @Override
    public void feedbackCallback(AjaxRequestTarget target) {
        selectedQueuedTaskModels.clear();
        dp.updateQueuedTasks();
        target.addComponent(grid);
    }

}
